/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: WebFilter
 * Author:   zhouheng
 * Date:     2019/4/4 11:59
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.onesquare.web.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author zhouheng
 * @create 2019/4/4
 * @since 1.0.0
 */
@Component
public class WebFilter extends ZuulFilter{

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        // 获取request上下文
        RequestContext requestContext = RequestContext.getCurrentContext();
        // 得到request域
        HttpServletRequest request = requestContext.getRequest();
        //得到头信息
        String header = request.getHeader("Authorization");
        //判断是否有头信息
        if(header != null && !"".equals(header)){
            //把头信息继续向下传
            requestContext.addZuulRequestHeader("Authorization", header);
        }

        return null;
    }
}